package com.yhans.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yhans.dao.MemberDao;
import com.yhans.pojo.Member;
import com.yhans.service.MemberService;
import com.yhans.utils.DaysOfMonthUtils;
import com.yhans.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    public Member findByTelephone(String telephone) {

        return memberDao.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {

        String password = member.getPassword();
        if (password!=null){
            //使用md5加密
            password = MD5Utils.md5(password);
            member.setPassword(password);
        }
        memberDao.add(member);

    }


    //根据月份查询会员数
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> list = new ArrayList<>();
        for(String m : months){   //yyyy.MM格式
            String daysOfMonth2 = DaysOfMonthUtils.getDaysOfMonth2(m);
            m = m +"."+daysOfMonth2;  //格式：2023.4 .18
//            m = m + ".28";//格式：2019.04.31
            Integer count = memberDao.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return list;
    }


}
